<?php

namespace App\Http\Controllers\Admin\Customer;

use App\Http\Controllers\Controller;
use App\Http\Requests\customer\PriceRequest;
use App\Models\customer\PriceModel;
use Illuminate\Http\Request;

class Price extends Controller
{
    use PriceRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $customer_id = $request->input('customer_id', '');
        $route_id = $request->input('route_id', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $query = PriceModel::query()->with(['customer']);

        if($customer_id){
            $query = $query->where('customer_id', $customer_id);
        }

        if($route_id){
            $query = $query->where('route_id', $route_id);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'warehouse' => $item->warehouse,
                'customer_id' => $item->customer_id,
                'customer_name' => $item->customer->name ?? '',
                'route_id' => $item->destination,
//                'route_name' => $item->route->arrive_port_code ?? '',
                'route_name' => $item->destination,
                'goods_attr' => $item->goods_attr,
                'currency_id' => $item->currency_id,
                'line_price' => $item->line_price,
                'transfer_price' => $item->transfer_price,
                'declare_price' => $item->declare_price,
                'thc_price' => $item->thc_price,
                'operate_price' => $item->operate_price,
                'other_price' => $item->other_price,
                'check_price' => $item->check_price,
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

//        $find = PriceModel::where('name', $params['name'])->first();
//        if($find){
//            return $this->error('名称已存在');
//        }

        $model = new PriceModel();
        $model->warehouse = $params['warehouse'] ?? '';
        $model->customer_id = $params['customer_id'];
//        $model->route_id = $params['route_id'];
        $model->destination = $params['route_id'];
        $model->goods_attr = $params['goods_attr'];
        $model->currency_id = $params['currency_id'];
        $model->line_price = $params['line_price'];
        $model->transfer_price = $params['transfer_price'] ?? 0;
        $model->declare_price = $params['declare_price'] ?? 0;
        $model->thc_price = $params['thc_price'] ?? 0;
        $model->operate_price = $params['operate_price'] ?? 0;
        $model->other_price = $params['other_price'] ?? 0;
        $model->check_price = $params['check_price'] ?? 0;

        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function update(Request $request, $id){
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = PriceModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

//        if($model->name != $params['name']){
//            $find = PriceModel::where('name', $params['name'])->first();
//            if($find){
//                return $this->error('名称已存在');
//            }
//        }

        $model->warehouse = $params['warehouse'] ?? '';
        $model->customer_id = $params['customer_id'];
//        $model->route_id = $params['route_id'];
        $model->destination = $params['route_id'];
        $model->goods_attr = $params['goods_attr'];
        $model->currency_id = $params['currency_id'];
        $model->line_price = $params['line_price'];
        $model->transfer_price = $params['transfer_price'] ?? 0;
        $model->declare_price = $params['declare_price'] ?? 0;
        $model->thc_price = $params['thc_price'] ?? 0;
        $model->operate_price = $params['operate_price'] ?? 0;
        $model->other_price = $params['other_price'] ?? 0;
        $model->check_price = $params['check_price'] ?? 0;
        $model->save();

        return $this->success();
    }


    public function delete(int $id){
        $model = PriceModel::find($id);
        if(!$model) {
            return $this->error('记录不存在');
        }
        $model->delete();
        return $this->success();
    }

}
